﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chili.Opf3;
using Chili.Opf3.Relations;
using Chili.Opf3.Storages.OleDb;
using Chili.Opf3.Storages;
using Chili.Opf3.Concurrency.Md5;
using Chili.Opf3.Query;

namespace DocManagerORM.OPF3
{
    public enum DBEnum
    { 
        NULL=0,Access,MsSql,Oracle,OleDb
    }

    public class OPF3Database 
    {

        private Chili.Opf3.Storages.Transaction  m_Transazione;

       public OPF3Database(DBEnum pDBType,string pConnString,bool pUsaAppConfigName=false)  
       {
           if (pUsaAppConfigName)
               ConnString =System.Configuration.ConfigurationManager.ConnectionStrings[pConnString].ConnectionString;
           else
                ConnString = pConnString;

           createDatabase(pDBType);
           //Contesto = ObjectContext.Factory.GetObjectContext() ;

           var ret = Contesto.GetObjectSet<Entità.Categorie>();
       }

       private void createDatabase(DBEnum pDBType)
       {
           switch (pDBType)
           {
               case DBEnum.NULL:
                   break;
               case DBEnum.Access:
                   Contesto = new ObjectContext(new AccessStorage(ConnString));
                   break;
               case DBEnum.MsSql:
                   Contesto = new ObjectContext(new Chili.Opf3.Storages.MsSql.MsSqlStorage(ConnString));
                   break;
               case DBEnum.Oracle:
                   Contesto = new ObjectContext(new Chili.Opf3.Storages.Oracle.OracleStorage(ConnString));
                   break;
               case DBEnum.OleDb:
                   Contesto = new ObjectContext(new Chili.Opf3.Storages.OleDb.OleDbStorage(ConnString));
                   break;
               default:
                   break;
           }

           // Create a concurrency manager to avoid multi user problems.
           Contesto.ConcurrencyManager = new Md5ConcurrencyManager();
       
       }//fM

       public string ConnString { get; set; }

       public DBEnum DBTipo { get; set; }

       public ObjectContext Contesto { get; set; }



       public bool SalvaDati<T>(T pObject)
       {
           m_Transazione = Contesto.StartTransaction() ; 

           try
           {
               Contesto.PersistChanges(pObject);
               m_Transazione.Commit();
               return true;
               
           }
           catch (Exception ex)
           {
               // Roll the update back.
               m_Transazione.Rollback();
               return false;
               //Console.WriteLine("We got an exception: " + ex.Message);
           }
       
       }//fM


       public T getObject<T>(string pWhere, params object[] pParams) where T :class
       {
           T oggetto = null;
           oggetto = Contesto.GetObject<T>(pWhere, pParams);

           return oggetto ;

       }//fM 

       public ObjectReader<T> getObjectDataReader<T>(string pWhere,params object[] pParams )
       {              
           ObjectReader<T> reader=null;
           if(string.IsNullOrEmpty(pWhere))
               reader = Contesto.GetObjectReader<T>();
           else
               reader = Contesto.GetObjectReader<T>(pWhere,pParams);

           return reader;    
       
       }//fM


       public  ObjectSet<T> getObjectSet<T>(string pWhere, params object[] pParams)
       {
           ObjectSet<T> reader = null;
           if (string.IsNullOrEmpty(pWhere))
               reader = Contesto.GetObjectSet<T>();
           else
               reader = Contesto.GetObjectSet<T>(pWhere, pParams);

           return reader;

       }//fM

      



    }//fC
}
